package ext;
import java.util.*;
import org.junit.*;
public class ParseNumberToChinese {
    String[] str = new String[]{"", "拾","百","千","万","拾万","百万","千万","亿","十亿","百亿","千亿"};

    public String parse(int num) {
        boolean flag = true;
        if (num == 0) {
            return "零";
        } else if (num < 0) {
            num = -num;
            flag = false;
        }
        StringBuilder sb = new StringBuilder();
        int idx = 0;
        Map<Integer, Character> map = new HashMap<>(){{
            put(1, '一');
            put(2, '二');
            put(3, '三');
            put(4, '四');
            put(5, '五');
            put(6, '六');
            put(7, '七');
            put(8, '八');
            put(9, '九');
        }};
        while (num > 0) {
            int cur = num % 10;
            if (cur == 0 && sb.charAt(0) != '零') {
                sb.insert(0, "零");
            } else if (cur != 0) {
                sb.insert(0, str[idx]);
                sb.insert(0, map.get(cur));
            }
            idx++;
            num /= 10;
        }
        if (!flag) {
            sb.insert(0, '负');
        }
        return filter(sb);
    }

    private String filter(StringBuilder sb) {
        boolean hasW = false, hasY = false;
        for (int i = sb.length() - 1; i >= 0; i--) {
            if (sb.charAt(i) == '万') {
                if (hasW) {
                    sb.deleteCharAt(i);
                } else {
                    hasW = true;
                }
            }
            if (sb.charAt(i) == '亿') {
                if (hasY) {
                    sb.deleteCharAt(i);
                } else {
                    hasY = true;
                }
            }
        }
        return sb.toString();
    }

    @Test
    public void test() {
        System.out.println(parse(1900074109));            
    }
}
